<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="120px"
      ref="dataForm"
      v-loading="loading"
    >
      <el-form-item
        :label="$t('monitor.ip')"
        prop="ip">
        <el-input
          :placeholder="$t('monitor.ip')"
          v-model="dataForm.ip"
        />
      </el-form-item>

      <el-form-item
        :label="$t('monitor.leader')"
        prop="leader">
        <el-select
          clearable
          filterable
          :placeholder="$t('monitor.leader')"
          v-model="dataForm.leader"
        >
          <el-option
            :key="item.value"
            :label="item.label"
            :value="item.value"
            v-for="item in userList"
          >
            <div class="leader-item">
              <span>{{ item.label }}</span>

              <span>{{ item.username }}</span>
            </div>
          </el-option>
        </el-select>
        <!--        <el-input-->
        <!--          :placeholder="$t('monitor.leader')"-->
        <!--          v-model="dataForm.leader"-->
        <!--        ></el-input>-->
      </el-form-item>

      <el-form-item
        :label="$t('monitor.apiMonitor')"
        prop="apiMonitor">
        <el-radio-group v-model="dataForm.apiMonitor">
          <el-radio :label="true">{{ $t("monitor.enable") }}</el-radio>
          <el-radio :label="false">{{ $t("monitor.disable") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('monitor.diskMonitor')"
        prop="diskMonitor">
        <el-radio-group v-model="dataForm.diskMonitor">
          <el-radio :label="true">{{ $t("monitor.enable") }}</el-radio>
          <el-radio :label="false">{{ $t("monitor.disable") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('monitor.processMonitor')"
        prop="processMonitor">
        <el-radio-group v-model="dataForm.processMonitor">
          <el-radio :label="true">{{ $t("monitor.enable") }}</el-radio>
          <el-radio :label="false">{{ $t("monitor.disable") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('monitor.memoryMonitor')"
        prop="memoryMonitor">
        <el-radio-group v-model="dataForm.memoryMonitor">
          <el-radio :label="true">{{ $t("monitor.enable") }}</el-radio>
          <el-radio :label="false">{{ $t("monitor.disable") }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        :label="$t('monitor.status')"
        prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio
            :label="true"
            border>有效</el-radio>
          <el-radio
            :label="false"
            border>无效</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button
        :loading="requesting"
        @click="visible = false">{{
          $t("cancel")
        }}</el-button>
      <el-button
        :loading="requesting"
        @click="dataFormSubmitHandle()"
        type="primary"
      >{{ $t("confirm") }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  name: 'ServerAddOrUpdate',
  data () {
    return {
      visible: false,
      loading: false,
      requesting: false,
      userList: [],
      dataForm: {
        ip: '',
        leader: '',
        apiMonitor: false,
        diskMonitor: false,
        memoryMonitor: false,
        processMonitor: false,
        status: true
      }
    }
  },
  computed: {
    dataRule () {
      return {
        ip: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        leader: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getUserList()
        // this.$refs.menuListTree.setCheckedKeys([])
        // this.$refs.deptListTree.setCheckedKeys([])
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getUserList () {
      this.$http
        .get(`/monitor/mmonitoriplist/userlist`)
        .then(({ data: res }) => {
          this.userList = res.data.map(item => {
            return {
              username: item.username,
              label: item.realName,
              value: item.id
            }
          })
        })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http
        .get(`/monitor/mmonitoriplist/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },

    dataFormSubmitHandle: debounce(
      function () {
        this.$refs['dataForm'].validate(valid => {
          if (!valid) {
            return false
          }
          // this.dataForm.menuIdList = [
          //   ...this.$refs.menuListTree.getHalfCheckedKeys(),
          //   ...this.$refs.menuListTree.getCheckedKeys()
          // ]
          // this.dataForm.deptIdList = this.$refs.deptListTree.getCheckedKeys()
          if (this.requesting) {
            return
          }
          this.requesting = true
          this.$http[!this.dataForm.id ? 'post' : 'put'](
            '/monitor/mmonitoriplist/',
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
            .catch(() => {})
            .finally(() => {
              this.requesting = false
            })
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style scoped lang="scss">
.leader-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  span {
    &:first-child {
    }
    &:last-child {
      font-size: 12px;
      color: #999999;
    }
  }
}
</style>
